{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "standard-spokesman",
   "metadata": {},
   "source": [
    "# Assignment II"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "polished-reader",
   "metadata": {},
   "source": [
    "## 4.1\n",
    "设总体的分布密度为\n",
    "$$\n",
    "f(x;\\alpha)=\n",
    "\\begin{cases}\n",
    "(\\alpha+1)x^{\\alpha},&0<x<1\\\\\n",
    "0,&ohters\n",
    "\\end{cases}\n",
    "$$\n",
    "\n",
    "$X_1,X_2,\\cdots,X_n$为样本，求参数$\\alpha$的矩估计和极大似然估计\n",
    "\n",
    "$x_1,x_2,\\cdots,x_6=0.1,0.2,0.9,0.8,0.7,0.7$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "remarkable-tourist",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "0.566666666666667"
      ],
      "text/latex": [
       "0.566666666666667"
      ],
      "text/markdown": [
       "0.566666666666667"
      ],
      "text/plain": [
       "[1] 0.5666667"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = c(0.1,0.2,0.9,0.8,0.7,0.7)\n",
    "n = length(x)\n",
    "x_bar = mean(x)\n",
    "x_bar"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "exact-progress",
   "metadata": {},
   "source": [
    "解：\n",
    "\n",
    "(1)矩估计\n",
    "\n",
    "$\\bar{X}=EX$\n",
    "\n",
    "$$\n",
    "EX=\\int_0^1 x(\\alpha+1)x^{\\alpha} dx=\\left[\\frac{(\\alpha+1)x^{\\alpha+2}}{\\alpha+2}\\right]_0^1=\\frac{\\alpha+1}{\\alpha+2}\n",
    "$$\n",
    "\n",
    "$$\n",
    "\\hat{\\alpha}=\\frac1{1-\\bar X}-2\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "anonymous-wellington",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "0.307692307692307"
      ],
      "text/latex": [
       "0.307692307692307"
      ],
      "text/markdown": [
       "0.307692307692307"
      ],
      "text/plain": [
       "[1] 0.3076923"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "alpha_hat = 1/(1-x_bar)-2\n",
    "alpha_hat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "statistical-construction",
   "metadata": {},
   "source": [
    "(2)极大似然估计\n",
    "\n",
    "解得：\n",
    "\n",
    "$$\n",
    "\\hat{\\alpha}=-1-\\frac1n \\sum_{i=1}^n \\ln{x_i}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "portable-championship",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "-0.174353839953726"
      ],
      "text/latex": [
       "-0.174353839953726"
      ],
      "text/markdown": [
       "-0.174353839953726"
      ],
      "text/plain": [
       "[1] -0.1743538"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "alpha_hat= -1-mean(log(x))\n",
    "alpha_hat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "accomplished-leeds",
   "metadata": {},
   "source": [
    "## 4.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "center-fields",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "20"
      ],
      "text/latex": [
       "20"
      ],
      "text/markdown": [
       "20"
      ],
      "text/plain": [
       "[1] 20"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = c(5,15,25,35,45,55,65)\n",
    "v = c(365,245,150,100,70,45,25)\n",
    "n = sum(v)\n",
    "x_bar = sum(x*v)/n\n",
    "x_bar"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "identified-window",
   "metadata": {},
   "source": [
    "### 利用optimize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "lovely-worship",
   "metadata": {},
   "outputs": [],
   "source": [
    "lnL=function(λ){\n",
    "    n*log(λ)-λ*sum(x*v)\n",
    "} "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "spiritual-surveillance",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAA0gAAANICAMAAADKOT/pAAAAMFBMVEUAAABNTU1oaGh8fHyM\njIyampqnp6eysrK9vb3Hx8fQ0NDZ2dnh4eHp6enw8PD////QFLu4AAAACXBIWXMAABJ0AAAS\ndAHeZh94AAAXiklEQVR4nO3da1caSxqA0QYRjQL+/397hBiPilz7rfveH2YcZ61Ud1U9QZuK\nTm/AbFPpC4AeCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkC\nCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkC\nCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkC\nCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkC\nCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkC\nCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCCAkCZAhpgsbcscvjwykwBEQSEgQQEgQQEgQQEgQQ\nEgQQEgQQEgQQEgQQEgQQEgQQEgQQEgQQEgQQEgQQEgQQEgQQEgQQEgQQEgQQEgQQEgToLKR7\nfioSzNdZSIcfiRd3HXCl7kJ60xIF9BjSm5bIrdOQ3rREVv2G9ObRA/l0HZKXJXLpPKQ3LZFF\n/yG9aYn0hgjpTUskNkpIb1oipYFCevMYj2SyhvT6tDr8lsDV+jXVEJdIiSQyhrRbfvmNmw9J\nhriGkkggY0jrafFnc/ho+7KY1imGuIoXJeJlDGkxbT4/3kyLFENcSUlEyxjSt1eC8y8LqXe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fp02usX8d/+Or2lwhv73/vfFttsP2X+aQjv4Cyvw30onhdtNDsfEfpm2+\nkI7GX05vT4vDl7dlxn/6+NIu01ckb5sfix+2/4S093x4gS8y/tP0J98XNr/N/+rwzX6p8d+e\n908bFs+Zxv8xuJDCxj/YLjJ9ZXk8/uGLiqIh7R82POZ6RfjtL5K9XC9IPwYXUtj4e7tFpi/s\nfvvSav/guWhI+++Rtrnefzga/3n/pd17yBlfkroIafHzuo8+kXn8vYds72Idjf94+JoyX0hH\n95/5L7Kj8ZfT/tuzXb43En/ca9j+K/LUbvvzqd0271O7b8Ntlw/53g38Of6cX0gfMX7ux/9H\n4+d+/P1zrLD9lzmkp8PfwC//v/939InM479/nO3rul/Gzx3Sifnf5pqEo/H/viJkex9r79tc\nh+2/0U82ZNtCJ8Y/KHiy4f27o93+e5Q/hcZfT/tzbutcf5HudXGy4f1r4r3D5v17Q18+UWL8\nx7yvCMf3//2j/OM/lZ3/j7NuOf82+zfbsfsvd0h/D/v+HXr68YkS42f+0ur4/r9/VGD8l4eS\n8/9x+jrb+G8/Q4raf7lDgi4JCQIICQIICQIICQIICQIICQIICQIICQIICQIICQIICQIICQII\nCQIICQIICQIICQIICQIICQIICQIICQIICQIICQIICQIICQIICQIICQIICQIICQIICQIICQII\nCQIICQIIqVkvpS+AL4TUqqWlq4nVaFXGXzzLZVajVUKqitVoVM5fxc5l1qJRQqqLtWiVjKpi\nNVolpKpYjVYJqSpWo1VCqorVaJWQqmI1WiWkqliNVk3TtvQl8D8htWo5TYvS18AnIbXqdSmk\niggJAggJAggJAggJAggJAggJAggJAggJAggJAggJAggJAggJAggJAggJAggJAggJAggJAggJ\nAggJAggJAggJAggJAggJAggJAggJAggJAggJAggJAggJAggJAggJAvwHYH8//G3/hasAAAAA\nSUVORK5CYII=",
      "text/plain": [
       "plot without title"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 420,
       "width": 420
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "λ = seq(0,1,.05)\n",
    "plot(λ,lnL(λ),type = 'l')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "upset-casting",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<dl>\n",
       "\t<dt>$maximum</dt>\n",
       "\t\t<dd>0.0500052032891922</dd>\n",
       "\t<dt>$objective</dt>\n",
       "\t\t<dd>-3995.73227896846</dd>\n",
       "</dl>\n"
      ],
      "text/latex": [
       "\\begin{description}\n",
       "\\item[\\$maximum] 0.0500052032891922\n",
       "\\item[\\$objective] -3995.73227896846\n",
       "\\end{description}\n"
      ],
      "text/markdown": [
       "$maximum\n",
       ":   0.0500052032891922\n",
       "$objective\n",
       ":   -3995.73227896846\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "$maximum\n",
       "[1] 0.0500052\n",
       "\n",
       "$objective\n",
       "[1] -3995.732\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "optimize(lnL,c(0,1),maximum = T)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "demonstrated-recording",
   "metadata": {},
   "source": [
    "### 利用uniroot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "enhanced-hungarian",
   "metadata": {},
   "outputs": [],
   "source": [
    "dlnL=function(λ){\n",
    "    n/λ-sum(x*v)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "standing-waste",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAA0gAAANICAMAAADKOT/pAAAAMFBMVEUAAABNTU1oaGh8fHyM\njIyampqnp6eysrK9vb3Hx8fQ0NDZ2dnh4eHp6enw8PD////QFLu4AAAACXBIWXMAABJ0AAAS\ndAHeZh94AAAUy0lEQVR4nO3d6VYiyRqG0UARJ8T7v9sWamhLS6XkzZhy7x9n0Z5VfKHymJAZ\naHkGLlZaLwBmICQIEBIECAkChAQBQoIAIUGAkCBASBAgJAgQEgQICQ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9AH808a7mx4eXV0OL5GuW80f1eO+9x2tX6QHk2xs+HlOfHR6cH74xN6\n9YEW82/qHhHef/5/3qo//7bt1//nXreaP81+fbWzj7/aIf3Y7PtjdHnzgRbzKz+1ev/5/3mr\nwfyH65Zf/5+7r6vNf34bUurxVzskmJKQIEBIECAkCBASBAgJAoQEAUKCACFBgJAgQEgQICQI\nEBIECAkChAQBQoIAIUGAkCBASBAgJAgQEgQICQKEBAFCggAhQYCQIEBIECAkCBASBAgJAoQE\nAUKCACFBgJAGdVUOrZfAK0Ia1EO5a70EXhHSqDZXrVfAK0Ia1W15ar0E/iekUT2Uqn8LnM8J\naVTXZdN6CfxPSIM6lFIeWi+C34Q0qNuyKzetF8FvQhrU5sqlpJ4IaUwP5fbloHTfehn8IqQx\nbV+ORofiUlI3hDSkQ7l+Pp64cympF0Ia0t3pWd39y/M7+iCkIV39+L65lNQNIY3o8eeZ75vy\n2Hgl/CQkCBASBAgJAoQEAUKCACFBgJAgQEgQICQIEBIECAkChAQBQoIAIUGAkCBASBAgJAgQ\nEgQICQKEBAFCggAhQYCQIEBIECAkCBASBAgJAoQEAUKCACFBgJAg4D/0PDaOrNck2AAAAABJ\nRU5ErkJggg==",
      "text/plain": [
       "plot without title"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 420,
       "width": 420
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "λ = seq(0,1,.05)\n",
    "plot(λ,dlnL(λ),type = 'l')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "breeding-partition",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<dl>\n",
       "\t<dt>$root</dt>\n",
       "\t\t<dd>0.0499913096427918</dd>\n",
       "\t<dt>$f.root</dt>\n",
       "\t\t<dd>3.47674716679467</dd>\n",
       "\t<dt>$iter</dt>\n",
       "\t\t<dd>10</dd>\n",
       "\t<dt>$init.it</dt>\n",
       "\t\t<dd>&lt;NA&gt;</dd>\n",
       "\t<dt>$estim.prec</dt>\n",
       "\t\t<dd>6.10351562500208e-05</dd>\n",
       "</dl>\n"
      ],
      "text/latex": [
       "\\begin{description}\n",
       "\\item[\\$root] 0.0499913096427918\n",
       "\\item[\\$f.root] 3.47674716679467\n",
       "\\item[\\$iter] 10\n",
       "\\item[\\$init.it] <NA>\n",
       "\\item[\\$estim.prec] 6.10351562500208e-05\n",
       "\\end{description}\n"
      ],
      "text/markdown": [
       "$root\n",
       ":   0.0499913096427918\n",
       "$f.root\n",
       ":   3.47674716679467\n",
       "$iter\n",
       ":   10\n",
       "$init.it\n",
       ":   &lt;NA&gt;\n",
       "$estim.prec\n",
       ":   6.10351562500208e-05\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "$root\n",
       "[1] 0.04999131\n",
       "\n",
       "$f.root\n",
       "[1] 3.476747\n",
       "\n",
       "$iter\n",
       "[1] 10\n",
       "\n",
       "$init.it\n",
       "[1] NA\n",
       "\n",
       "$estim.prec\n",
       "[1] 6.103516e-05\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "uniroot(dlnL,c(0,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "documented-column",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       ".list-inline {list-style: none; margin:0; padding: 0}\n",
       ".list-inline>li {display: inline-block}\n",
       ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "</style>\n",
       "<ol class=list-inline><li>1</li><li>-2</li><li>1</li></ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item 1\n",
       "\\item -2\n",
       "\\item 1\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. 1\n",
       "2. -2\n",
       "3. 1\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       "[1]  1 -2  1"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "3*c(-1,0,1)^2-2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "informative-bibliography",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "4.0.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
